package com.cskaoyan.market.admin.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import com.cskaoyan.market.admin.service.MarketAdminKeyWordService;
import com.cskaoyan.market.db.domain.MarketKeyword;
import com.cskaoyan.market.db.domain.MarketKeywordExample;
import com.cskaoyan.market.db.mapper.MarketKeywordMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;

/**
 * @Author:TaoRencan
 * @Date: 2023/3/21 11:19
 */
public class MarketAdminKeyWordServiceImpl implements MarketAdminKeyWordService {
    @Override
    public List<MarketKeyword> List(String page, String limit, String keyword, String url, String sort, String order) {
        SqlSession session = MyBatisUtil.getSession();
        MarketKeywordMapper mapper = session.getMapper(MarketKeywordMapper.class);
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (!StringUtils.isEmpty(url)) {
            criteria.andUrlLike("%" + url + "%");
        }
        marketKeywordExample.setOrderByClause(sort + " " + order);
        // 分页
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        criteria.andDeletedEqualTo(false);
        List<MarketKeyword> marketKeywordList = mapper.selectByExample(marketKeywordExample);
        session.commit();
        session.close();
        return marketKeywordList;

    }

    @Override
    public MarketKeyword create(String isDefault, String keyword, String url, String isHot) {
        SqlSession session = MyBatisUtil.getSession();
        MarketKeywordMapper mapper = session.getMapper(MarketKeywordMapper.class);
        MarketKeyword marketKeyword = new MarketKeyword();
        marketKeyword.setKeyword(keyword);
        marketKeyword.setAddTime(LocalDateTime.now());
        marketKeyword.setIsHot(Boolean.parseBoolean(isHot));
        marketKeyword.andLogicalDeleted(false);
        marketKeyword.setIsDefault(Boolean.parseBoolean(isDefault));
        marketKeyword.setUpdateTime(LocalDateTime.now());
        marketKeyword.setUrl(url == null ? "" : url);
        marketKeyword.setSortOrder(0);
        mapper.insert(marketKeyword);
        session.commit();
        session.close();
        return marketKeyword;
    }

    @Override
    public MarketKeyword update(String addTime, String id, String isDefault, String isHot, String keyword,
        String updateTime, String url) {
        SqlSession session = MyBatisUtil.getSession();
        MarketKeywordMapper mapper = session.getMapper(MarketKeywordMapper.class);
        MarketKeyword marketKeyword = mapper.selectByPrimaryKey(Integer.parseInt(id));
        // 字符串类型的转化为localTime ,输出
        marketKeyword
            .setAddTime((((LocalDateTime.parse(addTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))))));
        marketKeyword.setUpdateTime(LocalDateTime.now());
        marketKeyword.setUrl(url);
        marketKeyword.setIsDefault(Boolean.parseBoolean(isDefault));
        marketKeyword.setIsHot(Boolean.parseBoolean(isHot));
        marketKeyword.setKeyword(keyword);
        mapper.updateByPrimaryKey(marketKeyword);
        session.commit();
        session.close();
        return marketKeyword;

    }

    @Override
    public void delete(String addTime, String id, String isDefault, String isHot, String keyword, String updateTime,
        String url) {
        SqlSession session = MyBatisUtil.getSession();
        MarketKeywordMapper mapper = session.getMapper(MarketKeywordMapper.class);
        MarketKeyword marketKeyword = mapper.selectByPrimaryKey(Integer.parseInt(id));
        marketKeyword.setUpdateTime(LocalDateTime.now());
        marketKeyword.andLogicalDeleted(true);
        marketKeyword.setDeleted(true);
        mapper.updateByPrimaryKey(marketKeyword);
        session.commit();
        session.close();
    }
}